<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select("id,title,alias");
$query->from("#__categories");
$query->where("published=1 AND parent_id=1 AND extension='com_content'");
$query->order("rgt ASC, id DESC");
//echo $query;
$db->setQuery($query);
$lsCategories = $db->loadObjectList();
?>
<div id="menu">
	<ul id="toplevel">
	<?php
	foreach($lsCategories as &$row) {
		$link = JRoute::_("index.php?option=com_content&view=categories&id=".$row->id);
		$sql = "SELECT count(*) FROM #__categories WHERE published=1 AND parent_id=".$row->id;
		$db->setQuery($sql);
		$totalSubmenu = $db->loadResult();
		$sql = "SELECT * FROM #__categories WHERE published=1 AND parent_id=".$row->id." ORDER BY rgt ASC, id DESC";
		$db->setQuery($sql);
		$listSubmenu = $db->loadObjectList();
		if($totalSubmenu>0){
			?>
			<li id="p<?php echo $row->id;?>"><a href="<?php echo $link;?>" class="time fly" begin="p<?php echo $row->id;?>.mouseenter" end="p<?php echo $row->id;?>.mouseleave" timeaction="class:p1Hover"><?php echo $row->title;?></a>
				<ul class="time" begin="p<?php echo $row->id;?>.mouseenter" end="p<?php echo $row->id;?>.mouseleave" timeaction="display">
					<?php
					foreach($listSubmenu as &$submenu) {
						$linkSub = JRoute::_("index.php?option=com_content&view=categories&id=".$submenu->id);
						?>	
						<li><a href="<?php echo $linkSub;?>"><?php echo $submenu->title;?></a></li>	
						<?php 
					}	
					?>
				</ul>
			</li>
			<?php
		}else{
			?>
			<li><a href="<?php echo $link;?>"><?php echo $row->title;?></a></li>
			<?php
		}
	}
	?>
	</ul>
</div>
<script>
	$(document).ready(function() {
	   <?php
	   $count = count($lsCategories);
	   $height = $count*25;
	   ?>
	   $('#box_menu').css('margin-top', '<?php echo $height;?>px');
	});
</script>
